<?php

namespace app\api\controller;

use app\admin\model\xuboss\Carousel;
use app\admin\model\xuboss\Content;
use app\admin\model\xuboss\Menu;
use app\admin\model\xuboss\Notice;
use app\admin\model\xuboss\Video;
use app\common\controller\Api;
use think\cache\driver\Redis;
use think\exception\DbException;

class XubossApi extends Api
{
    protected $noNeedLogin = '*';
    protected $noNeedRight = '*';
    /**
     * @var array|mixed
     */
    private $params;
    private Redis $redis;

    public function _initialize()
    {
        parent::_initialize();
        $this->params = $this->request->param();
        if (empty($this->params['service_secret']))
            $this->error("service_secret不能为空");
        $this->redis = new Redis();
    }
    /**
     * 获取栏目
     * @return void
     */
    public function get_menu(){
        $redis_key = "{$this->params['service_secret']}:home:menu";
        $redis = $this->redis->get($redis_key);
        if(!empty($redis))
            $this->success(json_decode($redis,true));
        $arr = [];
        try {
            $arr = Menu::all(['state' => 1, 'service_secret' => $this->params['service_secret']]);
        } catch (DbException $e) {
            $this->error($e->getMessage());
        }
        $this->redis->set($redis_key,json_encode($arr),3600 * 24);
        $this->success($arr);
    }

    /**
     * 获取轮播图
     * @return void
     */
    public function home(){
        if (empty($this->params['menu_id']))
            $this->error("menu_id为空");
//        $redis_key = "{$this->params['service_secret']}:home:home";
//        $redis = $this->redis->get($redis_key);
//        if(!empty($redis))
//            $this->success(json_decode($redis,true));
        $carousel = [];
        try {
            $carousel = Carousel::all(['state' => 1,"menu_id"=>$this->params['menu_id'],
                'service_secret' => $this->params['service_secret']]);
        } catch (DbException $e) {
            $this->error($e->getMessage());
        }
        $notice = [];
        try {
            $notice = Notice::all(['state' => 1, "menu_id" => $this->params['menu_id'],
                'service_secret' => $this->params['service_secret']]);
        } catch (DbException $e) {
            $this->error($e->getMessage());
        }

        $video = [];
        try {
            $video = Video::all(['state' => 1, "menu_id" => $this->params['menu_id'],
                'service_secret' => $this->params['service_secret']]);
        } catch (DbException $e) {
            $this->error($e->getMessage());
        }
        if (!empty($video)){
            foreach ($video as $item) {
                $config = get_addon_config('qiniu');
                $item->url = $config['cdnurl'].$item->url;
            }
        }
        $content = [];
        try {
            $content = Content::all(['state' => 1, "menu_id" => $this->params['menu_id'],
                'service_secret' => $this->params['service_secret']]);
        } catch (DbException $e) {
            $this->error($e->getMessage());
        }
        $arr = [
            "carousel" => $carousel,
            "notice" => $notice,
            "video" => $video,
            "content" => $content,
        ];
//        $this->redis->set($redis_key,json_encode($arr),3600 * 24);
        $this->success($arr);
    }

}